package com.eastcom.collection.script

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress.Cache;
import java.sql.Date;
import java.sql.PreparedStatement
import java.sql.SQLException
import java.sql.Timestamp
import java.sql.Types;
import java.text.SimpleDateFormat
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

import com.eastcom.collection.tools.common.DateUtils;
import com.eastcom.collection.tools.db.DbEngine
import com.eastcom.groovy.provider.*
import com.eastcom.groovy.tools.mail.*
import com.sun.java.util.jar.pack.Instruction.Switch;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.sun.org.apache.bcel.internal.util.ClassPath.Dir;
import com.eastcom.collection.provider.*
import com.eastcom.collection.config.PmFile
import com.eastcom.collection.context.*
import com.eastcom.collection.tools.file.*

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPFile
import org.apache.commons.net.ftp.FTPFileFilter
import org.springframework.dao.DataAccessException
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback
import org.springframework.jdbc.support.lob.LobCreator
import org.springframework.jdbc.support.lob.OracleLobHandler
import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component
/**
 * 任务类，可配置
 * @author yuanyue
 *
 */
class SimpleTaskDirector_DurtyDataClear {
	/**
	 * 使用jaxb解析xml再入库
	 */
	/************************配置属性，可以修改，请尽量使用英文防止中文编码问题***************************/
	List<String> xmlFilePath
	String localRootPath
	final static String dstdbBean = "oracle_39_nhm"
	/***************************可配置部分结束***********************************************/
	final static String[] deleteDbSql = [
		"delete from nhm_pm_unit_collect_kpi_detail b where b.group_id in (select a.group_id from nhm_pm_unit_collect_kpi_group a where trunc(a.begintime,'DD') < trunc(sysdate,'DD') - 30)",
		"delete from nhm_pm_unit_collect_kpi_group a where trunc(a.begintime,'DD') < trunc(sysdate,'DD') - 30",
		"delete from nhm_pm_unit_collect_task c where trunc(c.timestamp,'DD') < trunc(sysdate,'DD') - 10 and c.status ='ok'"
	]

	final static String[] deleteDirPath = [
		"/home/ipnet/ipnet_unit_collection/data"
	]
	ThreadPoolTaskExecutor threadPoolTaskExecutor
	static Map<String,String> dstDbFeildAndComment = new HashMap<String, String>()
	static Log logger = LogFactory.getLog(SimpleTaskDirector_DurtyDataClear.class)

	/*********************initJob和doJob是要被程序调用的方法**************************/
	public static boolean initJob(String jobId){
		logger.debug("添加数据库和文件清理脚本。。。。。。")
	}
	public boolean doJob(String jobId){
		clearDatabase()
		clearDirFiles()
	}

	private void clearDatabase(){
		DbEngine dbEngine = SpringContextProvider.getBean("dbEngine")
		dbEngine.getJdbcUtil().setDataSource(SpringContextProvider.getBean(dstdbBean))
		if(deleteDbSql != null){
			deleteDbSql.each {
				try{
					dbEngine.getJdbcUtil().execute(it)
				}catch(Exception e){
					logger.error(this, e)
				}
			}
		}
	}

	private void clearDirFiles(){
		java.util.Date now = new java.util.Date()
		java.util.Date tm = DateUtils.getLastMonthFinalDay(now)
		if(deleteDirPath != null){
			deleteDirPath.each {
				try{
					List<File> files = new FileUtils().listDirAndSubDirFiles(it, null)
					if(files != null && files.size() > 0){
						files.each {
							if(it.lastModified() < tm.getTime()){
								it.delete()
							}
						}
					}
				}catch(Exception e){
					logger.error(this,e)
				}

			}
		}

	}
}


